package net.qiqbframework.commandhandling.callbacks;

import lombok.extern.slf4j.Slf4j;
import net.qiqbframework.commandhandling.CommandCallback;
import net.qiqbframework.commandhandling.CommandMessage;
import net.qiqbframework.commandhandling.CommandResultMessage;

import javax.annotation.Nonnull;

@Slf4j
public class LoggingCallback<C, R> implements CommandCallback<C, R> {


    public static final LoggingCallback<Object, Object> INSTANCE = new LoggingCallback<>();

    private LoggingCallback() {
    }

    @Override
    public void onResult(@Nonnull CommandMessage<? extends C> message, @Nonnull CommandResultMessage<? extends R> commandResultMessage) {
        if (commandResultMessage.isExceptional()) {
            log.warn("命令执行异常: {}",
                    message.getCommandName(),
                    commandResultMessage.exceptionResult());
        } else {
            log.info("命令执行成功: {}", message.getCommandName());
        }
    }
}
